package com.situ.car.dao.Impl;

import com.situ.car.dao.ICarDao;
import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.query.CarQuery;
import com.situ.car.pojo.vo.CarVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class CarDaoImpl implements ICarDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<CarVo> selectByPage(CarQuery carQuery) {
        String sql = "SELECT c.id AS id, c.license_plate AS licensePlate, c.image AS image, c.model AS model, c.details AS details, c.production_year AS year, c.color AS color, c.status AS status," +
                " c.rentStatus AS rentStatus, c.rental_price AS rentalPrice, c.on_sale_date AS onSaleTime,\n" +
                "       cc.category_name AS categoryName, cb.brand_name AS brandName\n" +
                "FROM car AS c\n" +
                "LEFT JOIN car_category AS cc ON c.car_category_id = cc.category_id\n" +
                "LEFT JOIN car_brand AS cb ON c.brand_id = cb.brand_id where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryModel = carQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and c.model like ?";
            queryList.add("%" + queryModel + "%");
        }
        String queryLicensePlate = carQuery.getLicensePlate();
        if (!ObjectUtils.isEmpty(queryLicensePlate)) {
            sql += " and c.license_plate like ?";
            queryList.add("%" + queryLicensePlate + "%");
        }
        Integer queryCategoryId = carQuery.getCategoryId();
        if (!ObjectUtils.isEmpty(queryCategoryId)) {
            sql += " and c.car_category_id=?";
            queryList.add(queryCategoryId);
        }
        Integer queryBrandId = carQuery.getBrandId();
        if (!ObjectUtils.isEmpty(queryBrandId)) {
            sql += " and c.brand_id=?";
            queryList.add(queryBrandId);
        }
        sql += " limit ?,?";
        int offset = (carQuery.getPage() - 1) * carQuery.getLimit();
        queryList.add(offset);
        queryList.add(carQuery.getLimit());
        List<CarVo> list = template.query(sql, new BeanPropertyRowMapper<>(CarVo.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(CarQuery carQuery) {
        String sql = "select count(*) \n" +
                "FROM car AS c\n" +
                "JOIN car_category AS cc ON c.car_category_id = cc.category_id\n" +
                "JOIN car_brand AS cb ON c.brand_id = cb.brand_id where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryModel = carQuery.getModel();
        if (!ObjectUtils.isEmpty(queryModel)) {
            sql += " and c.model like ?";
            queryList.add("%" + queryModel + "%");
        }
        String queryLicensePlate = carQuery.getLicensePlate();
        if (!ObjectUtils.isEmpty(queryLicensePlate)) {
            sql += " and c.license_plate like ?";
            queryList.add("%" + queryLicensePlate + "%");
        }
        Integer queryCategoryId = carQuery.getCategoryId();
        if (!ObjectUtils.isEmpty(queryCategoryId)) {
            sql += " and c.car_category_id=?";
            queryList.add(queryCategoryId);
        }
        Integer queryBrandId = carQuery.getBrandId();
        if (!ObjectUtils.isEmpty(queryBrandId)) {
            sql += " and c.brand_id=?";
            queryList.add(queryBrandId);
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        template.update("update car set status=? where id=?", status, id);
    }

    @Override
    public void add(Car car) {
        template.update("INSERT INTO car (brand_id, license_plate, model, image, production_year, color, details, rental_price, car_category_id) VALUES (?,?,?,?,?,?,?,?,?)",
                car.getBrandId(), car.getLicensePlate(), car.getModel(), car.getImage(), car.getYear(), car.getColor(), car.getDetails(), car.getRentalPrice(), car.getCategoryId());

    }

    @Override
    public void deleteById(Integer id) {
        template.update("delete from car where id=?", id);
    }

    @Override
    public Car selectById(Integer id) {
        return  template.queryForObject("SELECT id, license_plate, image, model, production_year AS year, color, details, status, rentStatus, rental_price, car_category_id AS categoryId, brand_id AS brandId\n" +
                "FROM car where id=?", new BeanPropertyRowMapper<>(Car.class), id);
    }

    @Override
    public Integer update(Car car) {
        try {
            return template.update("update car set brand_id=?, license_plate=?, model=?, image=?, production_year=?, color=?, details=?, rental_price=?, car_category_id=? where id=?"
                    , car.getBrandId(), car.getLicensePlate(), car.getModel(), car.getImage(), car.getYear(), car.getColor(), car.getDetails(), car.getRentalPrice(), car.getCategoryId(), car.getId());
        } catch (DuplicateKeyException e) {
            return 0;
        }
    }


    @Override
    public List<Car> selectOrder() {
        return template.query("select * from car where rentStatus=1 and status=1", new BeanPropertyRowMapper<>(Car.class));
    }

    @Override
    public List<Car> selectByRentalPrice(Integer carId) {
        return template.query("select rental_price from car where id=?", new BeanPropertyRowMapper<>(Car.class), carId);
    }

    @Override
    public void updateRentalStatus(Integer carId) {
        template.update("update car set rentStatus=0 where id=?", carId);
    }

    @Override
    public void updateRentalStatusUp(String licensePlate) {
        template.update("update car set rentStatus=1 where license_plate=?", licensePlate);
    }

    @Override
    public void updateCarStatus(Integer rentalId) {
        template.update("UPDATE car AS c\n" +
                "JOIN rental_order AS r ON c.id = r.car_id\n" +
                "SET c.rentStatus = 1\n" +
                "WHERE r.id = ?", rentalId);
    }

    @Override
    public Integer totalCost() {
        Integer count = template.queryForObject("select count(*) from car", Integer.class);
        return count;
    }

    @Override
    public Integer selectLicensePlateCount(Car car) {
        return template.queryForObject("select count(*) from car where license_plate=?", Integer.class,car.getLicensePlate());
    }
}
